import os
import re

def extract_files(md_content):
    pattern = r'\|\|\|FILE_PATH: (.*?)\|\|\|(.*?)\|\|\|END_FILE\|\|\|'
    matches = re.findall(pattern, md_content, re.DOTALL)
    return [(path.strip(), content.split('```', 2)[2].strip()) for path, content in matches]

def restore_repository(md_filepath, output_folder):
    with open(md_filepath, 'r') as f:
        md_content = f.read()

    files = extract_files(md_content)

    for file_path, file_content in files:
        full_path = os.path.join(output_folder, file_path)
        os.makedirs(os.path.dirname(full_path), exist_ok=True)
        with open(full_path, 'w') as f:
            f.write(file_content)

    print(f"Repository has been restored to: {output_folder}")

# 使用示例
md_filepath = "/path/to/merged_repo.md"
output_folder = "/path/to/restored_repo"
restore_repository(md_filepath, output_folder)
